﻿#ifndef EXPSCRIPT_H
#define EXPSCRIPT_H

#include <QtCore/qglobal.h>
#include <stdio.h>
#include "../sys_h/DATATYPE.h"
#include "../sys_h/DATATYPE_S.h"
#include "../sh_m_h/NSA3000TRD.h"

#if defined(EXPSCRIPT_LIBRARY)
#  define EXPSCRIPTSHARED_EXPORT Q_DECL_EXPORT
#else
#  define EXPSCRIPTSHARED_EXPORT Q_DECL_IMPORT
#endif

#pragma pack(1)

#define  MAX_EXP_VAR_NUM    30			//最大变量数
//变量值数据类型
#define  VAR_TYPE_CHAR       1           /*   c   */
#define  VAR_TYPE_SHORTINT   2			/*   s   */
#define  VAR_TYPE_INT        3			/*   i   */
#define  VAR_TYPE_FLOAT      4			/*   f   */
#define  VAR_TYPE_DOUBLE     5			/*   d   */


typedef struct _Var_DataVal
{
	XCHAR    Value[8];
	XUCHAR   data_type;
	_Var_DataVal()
	{
		memset(Value, 0, 8);
		data_type = 0;
	}
	void Clear()
	{
		memset(Value, 0, 8);
		data_type = 0;
	}
}Var_DataVal;

//存输入量的输入值
typedef struct _Input_Var_Value
{
	Var_DataVal		InputVal;
	XBOOL8          bNewData;
	_Input_Var_Value()
	{
		InputVal.Clear();
		bNewData = false;
	}


}Input_Var_Value;

typedef struct _Var_DataDef
{
	XCHAR   ByName[BYNAME_LEN];
	XINT    TableID;
	XINT    IndexNo;
	XINT    ColumnID;
	_Var_DataDef()
	{
		memset(ByName, 0, BYNAME_LEN);
		TableID=0;
		IndexNo=0;
		ColumnID=0;
	}
	void Clear()
	{
		memset(ByName, 0, BYNAME_LEN);
		TableID=0;
		IndexNo=0;
		ColumnID=0;
	}
}Var_DataDef;

//输入变量索引
typedef struct _Var_Index
{
	XINT	ParentId;
	XINT	VarId;
	XINT	VarRealId;
	_Var_Index()
	{
		ParentId=0;
		VarId=0;
		VarRealId=0;
	}
	void Clear()
	{
		ParentId=0;
		VarId=0;
		VarRealId=0;
	}
}Var_Index;				

typedef struct _Script_Struct
{
	XINT			InputVarNum;
	Var_DataDef		InputVarDef[MAX_EXP_VAR_NUM];
	Var_Index		InputVarIndex[MAX_EXP_VAR_NUM];
	XCHAR			Script[STRING_CHAR_MAX_LEN];
	Var_DataDef		OutPutVarDef;
	_Script_Struct()
	{
		InputVarNum = 0;
		for (XINT i=0; i<MAX_EXP_VAR_NUM; i++)
		{
			InputVarDef[i].Clear();
			InputVarIndex[i].Clear();
		}
		memset(Script, 0, STRING_CHAR_MAX_LEN);
		OutPutVarDef.Clear();
	}
}Script_Struct;

#pragma pack()

class EXPSCRIPTSHARED_EXPORT ExpScript 
{
public:
    ExpScript();
	~ExpScript();
public:
	XINT Init_proc(NSA3000TRD *pRealDB);

	XINT SetComputeScript(Script_Struct *pScriptStruct);
	//校验脚本是否OK
	XINT CheckScript(QString &ErrInfo);

	//设置输入量的值
	void SetInputVarValue(Input_Var_Value *pVarValueList, XINT ValueNum);

	//计算脚本
	XINT ComputeScript();

	//清输入量的值
	void ClearInputVarValue();

	XINT GetComputeResult(Var_DataVal *res_value);

protected:
	XINT GetInputVarValrue(XINT VarNo, Var_DataVal *pVarVal);

private:
	Script_Struct	*m_pScriptStruct;
	Var_DataVal		m_ResultVal;
	NSA3000TRD      *m_pRealDB;
	Input_Var_Value *m_Var_Data;
	XINT			m_Var_Num;

};

#endif // EXPSCRIPT_H
